package org.jypj.dev.controller;

import org.jypj.dev.service.IEmergeFileService;
import org.jypj.dev.service.OeoProblemService;
import org.jypj.dev.util.ImageUtil;
import org.jypj.dev.util.Mp3Util;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.vo.EmergyUser;
import org.jypj.dev.vo.SearchVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * EmergeFile控制器
 *
 * @author ChenYu
 */
@Controller
@RequestMapping("emergeFileController")
public class EmergeFileController extends BaseController {

    @Resource
    private IEmergeFileService emergeFileService;

    @Resource
    private OeoProblemService oeoProblemService;

    @RequestMapping(value = "toEmergeFileList.action")
    public String toEmergeFileList(HttpServletRequest request) {
        initTodayAndTom(request);
        return "back/emerge/emerge_main";
    }

    /**
     * 首页工序预览跳转到应急
     *
     * @param request
     * @param searchVo
     * @return
     */
    @RequestMapping(value = "toIndexEmergeFileList.action")
    public String toIndexEmergeFileList(HttpServletRequest request, SearchVo searchVo) {
        request.setAttribute("searchVo", searchVo);
        return "back/bus/index_emerge_list";
    }

    /**
     * 跳转到181故障列表页
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "toProblem181List.action")
    public String toProblem181List(HttpServletRequest request) {
        initTodayAndTom(request);
        return "back/emerge/problem_181_list";
    }

    /**
     * 分页查询181故障
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectOnePage.action")
    public Page selectOnePage(Page page, String searchKey, String startDate, String endDate) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("searchKey", searchKey);
        queryMap.put("startDate", startDate);
        queryMap.put("endDate", endDate);
        page = oeoProblemService.selectOnePageByMap(page, queryMap);
        return page;
    }

    /**
     * 查询所有数据接口
     *
     * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
     */
    @RequestMapping("selectAllEmergyUser.action")
    @ResponseBody
    public Map<String, Object> selectAllEmergyUser(String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("startDate", startDate);
            queryMap.put("endDate", endDate);
            List<EmergyUser> eus = emergeFileService.selectAllEmergyUser(queryMap);
            if (eus.size() == 0) {
                code = -2;
            }
            map.put("datas", eus);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 查询所有紧急文件
     *
     * @return
     */
    @RequestMapping("selectAllEmergeFile.action")
    @ResponseBody
    public Map<String, Object> selectAllEmergeFile(String startDate, String endDate, String id, String searchCode, String userNum) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("id", id);
            queryMap.put("searchCode", searchCode);
            queryMap.put("startDate", startDate);
            queryMap.put("userNum", userNum);
            queryMap.put("endDate", endDate);
            List<EmergyUser> eus = emergeFileService.selectAllEmergeFile(queryMap);
            map.put("datas", eus);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 改变mp3
     *
     * @param imagePath
     * @return
     */
    @RequestMapping("changeMp3.action")
    @ResponseBody
    public Map<String, Object> changeMp3(String imagePath) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            String file_path = PropertyUtil.getProperties("file_path");
            String file_server_url = PropertyUtil.getProperties("file_server_url");
            int i = imagePath.lastIndexOf("/");
            String substring = imagePath.substring(i + 1);
            String targetPath = file_path + "/mp3change/" + substring + ".mp3";
            File file = new File(targetPath);
            if (!file.exists()) {
                Mp3Util.changeToMp3(file_path + imagePath, file_path + "/mp3change/" + substring + ".mp3");
            }
            map.put("url", file_server_url + "/mp3change/" + substring + ".mp3");
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /***
     * Mp3预览
     * @param imagePath
     * @param request
     * @return
     */
    @RequestMapping("toMp3Listen.action")
    public String toMp3Listen(String imagePath, HttpServletRequest request) {
        String file_path = PropertyUtil.getProperties("file_path");
        String file_server_url = PropertyUtil.getProperties("file_server_url");
        Mp3Util.changeToMp3(file_path + imagePath, file_path + imagePath);
        request.setAttribute("mp3Url", file_server_url + imagePath);
        return "index";
    }

    /**
     * 下载图片
     */
    @RequestMapping("downloadImage.action")
    public void downloadImage(HttpServletResponse response, String imagePath) {
        ImageUtil.downloadImage(response, "应急图片.jpg", imagePath);
    }
}